package com.aivin.lib_widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.TextureView;


public class MyTextureView extends TextureView {
    public MyTextureView(Context context) {
        super(context);
        init();
    }

    public MyTextureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {

    }

    private float scaleK = -1;

    public float getSacaleK() {
        if (scaleK == -1) {
            scaleK = getWidth() * 1.0f / getHeight();
        }
        return scaleK;
    }

    private float downX, downY;


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getRawX();
                downY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getRawX();
                float moveY = event.getRawY();
                onLocationChangeCallBack.onlocationChange((int) (moveX - downX), (int) (moveY - downY));
                downX = moveX; // important
                downY = moveY;
                break;
            case MotionEvent.ACTION_UP:
                performClick();
                break;
        }
        return true;
    }


    @Override
    public boolean performClick() {
        return super.performClick();
    }


    private OnLocationChangeCallBack onLocationChangeCallBack;

    public void setOnLocationChangeCallBack(OnLocationChangeCallBack onLocationChangeCallBack) {
        this.onLocationChangeCallBack = onLocationChangeCallBack;
    }

    public interface OnLocationChangeCallBack {
        void onlocationChange(int x, int y);
    }
}
